package top.malaoshi.netdisk.jdbc;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import top.malaoshi.netdisk.component.NetdisConfigReader;
import top.malaoshi.sprout.jdbc.DbTemplate;
import top.malaoshi.sprout.jdbc.DbTemplateFactory;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
@Order(300)
public class DbConfig {
    @Resource
    private NetdisConfigReader ncr;

    @Bean
    public DbTemplate dbTemplate() throws Exception {
        Map configMap = ncr.getConfigMap();

        DataSource dataSource = DruidDataSourceFactory.createDataSource(configMap);

        DbTemplate dbTemplate = DbTemplateFactory.build(dataSource);

        return dbTemplate;
    }
}
